<?php

require('init.php');
require(LIBWWWDIR . '/header.php');
$title = 'SourceCode: ' . htmlspecialchars($sourcefile);
require(LIBWWWDIR . '/highlight.php');

if ( !isset($_REQUEST['id']) || empty($_REQUEST['id']) )
	header('Location: index.php');

//buscar code en db2
$scid = $_REQUEST['id'];
$source = get_source('1', '', $scid, $conn_db2);
$source =$source[1];

if ( strlen($source['sourcecode'] ) == 0 )
{
	// Someone submitted an empty file. Cope gracefully.
	echo "<p class=\"nodata\">empty file</p>\n\n";

}elseif( strlen( $source['sourcecode'] ) < 10 * 1024 ){

	// Source < 10kB (for longer source code, highlighter tends to take very long time or timeout)
	$datos_team = exist("login", $source['teamid'], "team", $conn_db2);
	$name = $datos_team[1]['name'];
	echo '<h2 class="filename"><a name="source"></a>Problem SourceCode : '.$source['probid'].
	" (<a href=\"new_show_source.php?id=".$scid."&amp;fetch=1\">download</a>)</h2>\n\n".
	 'By team: '.'<strong>'.utf8_encode($name).'</strong>';
	highlight($source['sourcecode'], $source['langid']);

	//buscar comentarios en db2 si existen commentarios asociados al sc seleccionado
	$comm = exist("scid", $scid, "comments", $conn_db2);

	if ( $comm[0] == 0 )
	{ 
		echo "<h2>Comentarios: </h2>";
		echo '<table><tr><td> No se han adicionado Comentarios'." ".	
		'</td></tr></table>';
	}else{
		echo "<h1>Comentarios: </h1>";
			
		for($i = 1; $i<=$comm[0]; $i++)
		{
			$team_name = get_teamname($comm[$i]['teamid'], $conn_db2);
			echo '<table><tr><td>'.$comm[$i]['submittime']."  team: ".
			"<a href=\"team.php?id=".$comm[$i]['teamid']."\">".utf8_encode($team_name['name']).
			"</a></td></tr><tr><td>".'<pre class="output_text">' . 
			htmlspecialchars(wrap_unquoted($comm[$i]['body'],80)) . 
			"</br></pre></td></tr></table>";		
		}
	}
}else{
	echo '<h2 class="filename"><a name="source"></a>SourceCode: '. " (<a " .
	"href=\"show_source.php?id=$id&amp;fetch=1\">download</a>)</h2>\n\n";

	highlight_native( $source['sourcecode'], $source['langid'] );
	
	//buscar en db2 si existen commentarios asociados al sc seleccionado
	$comm = exist("scid", $scid, "comments", $conn_db2);
		
	if ( $comm[0] == 0 )
	{ 
		echo "<h2>Comentarios: </h2>";
		echo '<table><tr><td> No se han adicionado Comentarios'." ".	
		'</td></tr></table>';
	}else{
		echo "<h1>Comentarios: </h1>";
		for($i = 1; $i<=$comm[0]; $i++)
		{
			$team_name = get_teamname($comm[$i]['teamid'], $conn_db2);
			echo '<table><tr><td>'.$comm[$i]['submittime']."team: ".
			"<a href=\"team.php?id=".$comm[$i]['teamid']."\">".utf8_encode($team_name['name']).
			"</a></td></tr><tr><td>".'<pre class="output_text">' . 
			htmlspecialchars(wrap_unquoted($comm[$i]['body'],80)) . 
			"</br></pre></td></tr></table>";		
		}
	}
}

require(LIBWWWDIR . '/footer.php');
